package com.njy.cloudalibaba.consumer.controller;

import com.njy.config.JdkThreadPoolExecutor;
import com.njy.pojo.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class AsyncMethod {
    private final static Logger LOGGER = LoggerFactory.getLogger(AsyncMethod.class);

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private JdkThreadPoolExecutor jdkThreadPoolExecutor;

    @Async
    public void testAsyncMethod() {
        try {
            Thread.sleep(5000);
            //LOGGER.info("子线程testAsyncMethod方法" + MDC.get(Constant.TRACE_ID));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void testThreadPoolMethod() {
        //threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                  //  LOGGER.info("子线程testThreadPoolMethod方法" + MDC.get(Constant.TRACE_ID));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void testThreadPoolTaskMethod2() {
        /*threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    LOGGER.info("子线程testThreadPoolMethod2方法" + MDC.get(Constant.TRACE_ID));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });*/
        AtomicInteger atomicInteger = new AtomicInteger(0);
        for (int i = 0; i < 50; i++) {
            threadPoolTaskExecutor.execute(() -> {
                //LOGGER.info(Thread.currentThread().getName()+"--->{}",atomicInteger.incrementAndGet());
                //LOGGER.info("子线程testThreadPoolMethod4方法"+ MDC.get(Constant.TRACE_ID));
            });
        }
        System.out.println(atomicInteger.get());
    }

    public void testThreadPoolTaskMethod3() {
        threadPoolTaskScheduler.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    //LOGGER.info("子线程testThreadPoolMethod3方法" + MDC.get(Constant.TRACE_ID));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void testThreadPoolTaskMethod4() {
        //AtomicInteger atomicInteger = new AtomicInteger(0);
        //for (int i = 0; i < 50; i++) {
        //    jdkThreadPoolExecutor.execute(() -> {
        //            LOGGER.info(Thread.currentThread().getName()+"--->{}",atomicInteger.incrementAndGet());
        //            //LOGGER.info("子线程testThreadPoolMethod4方法"+ MDC.get(Constant.TRACE_ID));
        //    });
        //}
        //System.out.println(atomicInteger.get());
    }

}
